<?php
namespace Chat\Model;
use Think\Model;
class ChatFeeTurnoverModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_chat_fee'; 
	
	
	public function getTurnoverList($data){
		$map = array();
		if(!empty($data['custid'])){
			$map['custid'] = $data['custid'];
		}
		
		if(!empty($data['chatid'])){
			$map['chatid'] = $data['chatid'];
		}
		
		if(!empty($data['direction'])){
			$map['direction'] = $data['direction'];
		}
		
		$total = $this->where($map)->count();
		$fields = array('id','chatid','custid','amount','direction','content','createtime');
		
		if(empty($data['sortField']))$data['sortField'] = "id";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	public function getLeftAmount($chatid){
		$chatid = (int)$chatid;
		$result = $this->query("select sum(amount) as amount,direction from qyx_chat_fee where chatid={$chatid} group by direction");
		return $result;
	}
	
	public function getInfoById($id){
		$map['id'] = (int)$id;
		$result = $this->where($map)->find();
		return $result;
	}
	
	public function addTurnover($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function delTurnover($data){
		$map['id'] = $data['id'];
		$map['chatid'] = $data['chatid'];
		$result = $this->where($map)->delete();
		return $result;
	}
}


